<template>
  <div class="page">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="demo-ruleForm">
      <el-form-item label="用户名"
                    prop="UserName">
        <el-input v-model="ruleForm.UserName"></el-input>
      </el-form-item>
    <el-form-item label="密码"
                    prop="PassWord">
        <el-input v-model="ruleForm.PassWord"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-link type="primary"  href="/#/register">注册</el-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  data() {
    return {
      ruleForm: {
        UserName: '',
        PassWord: ''
      },
      rules: {
        UserName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        
        ],
        PassWord: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
    }
  },
  components: {},
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              //验证成功
              //判断用户名和密码
              this.$axios.post('http://localhost:54080/api/login?uname='+this.ruleForm.UserName+'&upass='+this.ruleForm.PassWord+''
              ).then(res=>{
                  if(res.data.code>0){
                      this.$message.success('登录成功');
                      this.$router.push('/home/main')
                  }else{
                      this.$message.error('登录失败，用户名或密码错误');
                  }
              })
            //alert('submit!');
          } else {
              //验证失败
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style scoped lang="stylus"></style>
